/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.platform.user.service.impl;

import com.hyts.infodb.path.InfodbConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import com.hyts.infodb.config.SystemConfiguration.MessageConfiguration;
import com.hyts.infodb.config.SystemConfiguration.ResultConfiguration;
import com.hyts.infodb.platform.user.bean.LoginParamDTO;
import com.hyts.infodb.platform.user.bean.LoginResultDTO;
import com.hyts.infodb.platform.user.service.LoginService;
import com.hyts.infodb.user.bean.UserBO;
import com.hyts.infodb.user.bean.UserDTO;

/** 
 * @author LiBo/Alex
 * @see com.hyts.infodb.platform.user.service.LoginService
 * @version V1.0 
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    InfodbConfiguration infodbConfiguration;
    /*
     * @param dto
     * @return
     * @see com.hyts.infodb.platform.user.service.LoginService#login(com.hyts.infodb.platform.user.bean.LoginParamDTO)  
     * @exception
     */ 
    @Override
    public LoginResultDTO login(LoginParamDTO dto) {
        LoginResultDTO resultDTO = new LoginResultDTO();
        //用户名或者密码为空校验
        if(StringUtils.isEmpty(dto.getUserName()) && StringUtils.isEmpty(dto.getEmail())) {
        	resultDTO.failure(MessageConfiguration.LOGIN);
        	resultDTO.setMessage("用户名或者邮箱不允许为空！");//覆盖原有操作，否则会输出默认语句
            return resultDTO;
        }else if(StringUtils.isEmpty(dto.getPassword())){
        	resultDTO.failure(MessageConfiguration.LOGIN);
            resultDTO.setMessage("密码不允许为空！");
            return resultDTO;
        }
        UserBO bo = new UserBO();
        bo.setUserName(dto.getUserName()).setPassword(dto.getPassword());
        UserDTO result = restTemplate.postForObject(infodbConfiguration.getUserAddress()+"user/login", bo, UserDTO.class);
        if(result.getCode() == ResultConfiguration.SUCCESS.getCode()) {
        	resultDTO.success(MessageConfiguration.LOGIN);
        	resultDTO.setMessage(result.getMessage());
        }else if(result.getCode() == ResultConfiguration.FAILURE.getCode()) {
        	resultDTO.failure(MessageConfiguration.LOGIN);
        	resultDTO.setMessage(result.getMessage());
        }
        return resultDTO;
    }

    /*
     * @param dto
     * @return
     * @see com.hyts.infodb.platform.user.service.LoginService#logout(com.hyts.infodb.platform.user.bean.LoginParamDTO)  
     * @exception
     */ 
    @Override
    public LoginResultDTO logout(LoginParamDTO dto) {
        return null;
    }
}
